package cn.com.bsoft.mobile.ui

import cn.com.bsoft.mobile.model.AppVersionRsp
import cn.com.bsoft.mobile.model.DeviceType
import cn.com.bsoft.mobile.service.AppVersionService
import cn.com.bsoft.mobile.service.AppVersionUploadReceiver
import cn.com.bsoft.mobile.service.MobileAppService
import com.github.vok.karibudsl.*
import com.vaadin.data.provider.ListDataProvider
import com.vaadin.navigator.View
import com.vaadin.shared.ui.MarginInfo
import com.vaadin.spring.annotation.SpringView
import com.vaadin.ui.Alignment
import com.vaadin.ui.Upload
import com.vaadin.ui.VerticalLayout
import com.vaadin.ui.themes.ValoTheme
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.context.WebApplicationContext

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.OutputStream
import java.util.*
import javax.annotation.PostConstruct
import com.vaadin.ui.renderers.ButtonRenderer


@SpringView(name = "AppManager")
class AppVersionManagerView : VerticalLayout(), View {

    @Autowired
    lateinit var appService: MobileAppService

    @Autowired
    lateinit var versionService: AppVersionService

    @Autowired
    lateinit var applicationContext: WebApplicationContext



    @PostConstruct
    fun setup() {
        isSpacing = false;setSizeFull()

        MarginInfo(false, false, false, false)

        tabSheet {
            margin = MarginInfo(false, true, true, true)
            setSizeFull()
            verticalLayout {
                //expandRatio = 1.0f
                //addStyleName("backgroundBlue")
                isSpacing = false
                setSizeFull()
                margin = MarginInfo(false, false, false, false)
                tab.caption = "上传"
                val upload = ArchiveUpload()
                addComponent(upload)

                setExpandRatio(upload,1.0f)
            }
            panel {
                tab.caption = "应用套件"
                isSpacing = false
                addStyleName(ValoTheme.PANEL_BORDERLESS)

                //margin = MarginInfo(false, false, false, false)
                setSizeFull()
                content = verticalLayout {
                    setSizeUndefined()
                    w = fillParent
                    //expandRatio = 1.0f
                    //addStyleName("backgroundBlue")
                    isSpacing = false

                    margin = MarginInfo(false, false, false, false)

                    val c = AppSuiteList()
                    addComponent(c)

                    //setExpandRatio(upload,1.0f)
                }
            }


            verticalLayout {
                //expandRatio = 1.0f
                //addStyleName("backgroundBlue")
                isSpacing = false
                setSizeFull()
                margin = MarginInfo(false, false, false, false)
                tab.caption = "版本管理"
                val c = VersionManager()
                addComponent(c)

                //setExpandRatio(upload,1.0f)
            }

            getTab(0).isEnabled = true
        }

//            label("应用版本管理") {
//                w = fillParent; styleName = ValoTheme.LABEL_HUGE
//            }
//            horizontalLayout {
//                //alignment = Alignment.BOTTOM_LEFT
//                val cbApp = comboBox<String>(caption = "应用") {
//
//                    val map = HashMap<String, String?>()
//
//                    appService.allApp().forEach {
//                        map[it.nativeID] = it.title
//                    }
//
//                    setItems({ caption: String, text: String -> caption.contains(text, ignoreCase = true) },
//                            map.keys)
//
//                    setItemCaptionGenerator {
//                        map[it]
//                    }
//
//                    isEmptySelectionAllowed = false
//                    isTextInputAllowed = false
//
//                }
//                val cbType = comboBox<String>(caption = "设备类型") {
//                    setItems({ caption: String, text: String -> caption.contains(text, ignoreCase = true) },
//                            listOf(DeviceType.Android.value, DeviceType.iOS.value))
//
//                    setItemCaptionGenerator {
//                        if (it != "") it else "全部"
//                    }
//
//                    isEmptySelectionAllowed = true
//                    isTextInputAllowed = false
//                }
//
//                button("查询") {
//                    alignment = Alignment.BOTTOM_LEFT
//                    onLeftClick {
//                        val appID = cbApp.value
//                        val type = cbType.value
//
//                        versions.clear()
//                        versions.addAll(versionService.queryVersion(appID, type))
//
//                        dataProvider.refreshAll()
//                    }
//                }
//
//
//            }
//            grid(AppVersionRsp::class, "", dataProvider) {
//                w = fillParent
//                showColumns(AppVersionRsp::version,
//                        AppVersionRsp::type,
//                        AppVersionRsp::createDate,
//                        AppVersionRsp::describe,
//                        AppVersionRsp::enable)
//
//                addColumn({ if (true == it.enable) "禁用" else "启用" },
//                        ButtonRenderer { clickEvent ->
//                            val item = clickEvent.item!!
//                            versionService.toggleVersion(item.type!!.value,item.appID!!,item.version!!)
//                            item.enable = !item.enable!!
//                            dataProvider.refreshItem(clickEvent.item)
//                        })
//
//                addColumn({"删除"},
//                        ButtonRenderer { clickEvent ->
//                            val item = clickEvent.item!!
//                            versionService.removeVersion(item.type!!.value,item.appID!!,item.version!!)
//                            versions.remove(item)
//                            dataProvider.refreshAll()
//                        })
//            }


    }
}